#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"

void STInit(St* ps)
{
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
void STDestroy(St* ps)
{
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
void STCapacityCheck(St* ps)
{
	if (ps->top == ps->capacity)
	{
		int NewCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, NewCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = NewCapacity;
	}
}

void STPush(St* ps, STDataType x)
{
	assert(ps);
	STCapacityCheck(ps);
	ps->arr[ps->top++] = x;
}
bool STEmpty(St* ps)
{
	assert(ps);
	return ps->top == 0;
}
void STPop(St* ps)
{
	assert(!(STEmpty(ps)));
	ps->top--;
}

STDataType STTop(St* ps)
{
	return ps->arr[ps->top - 1];
}

int STSize(St* ps)
{
	return ps->top;
}